package com.jintoufs.util;

import freemarker.template.Template;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 发送邮件
 * @author tcq
 *
 */
public class MailSendUtils {

	private String mailHost;
	private String mailUserName;
	private String mailPassword;

	private FreeMarkerConfigurer freeMarkerConfigurer;//FreeMarker的技术类

	public FreeMarkerConfigurer getFreeMarkerConfigurer() {
		return freeMarkerConfigurer;
	}

	public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
		this.freeMarkerConfigurer = freeMarkerConfigurer;
	}


	public String getMailAddress() {
		return mailAddress;
	}

	public void setMailAddress(String mailAddress) {
		this.mailAddress = mailAddress;
	}

	private String mailAddress;

	public String getMailHost() {
		return mailHost;
	}

	public void setMailHost(String mailHost) {
		this.mailHost = mailHost;
	}

	public String getMailUserName() {
		return mailUserName;
	}

	public void setMailUserName(String mailUserName) {
		this.mailUserName = mailUserName;
	}

	public String getMailPassword() {
		return mailPassword;
	}

	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}

	//创建线程池
	private ExecutorService executorService = Executors.newFixedThreadPool(100);//创建一个100根线程的线程池

	public void sendMail(final String subject, final Map<String,Object> root, final String to,final String templateName) {
		//创建一个新线程来发送电子邮件
		executorService.execute(new Runnable() {
			public void run() {
				//从创建邮件会话
				Session session = Session.getDefaultInstance(new Properties());
				MimeMessage message = new MimeMessage(session);
				try {
					System.out.println("开始发送......");
					//发件人
					message.setFrom(new InternetAddress(mailAddress));
					//收件人
					message.addRecipient(RecipientType.TO, new InternetAddress(to));
					//邮件标题
					message.setSubject(subject);
					//发送日期
					message.setSentDate(new Date());
					//邮件内容对象
					MimeBodyPart body = new MimeBodyPart();
					//设置邮件内容
					String content=getMailText(root,templateName);//使用模板生成html邮件内容
					body.setContent(content, "text/html;charset=gb2312");
					//将邮件内容和附件之类的封装起来
					Multipart multipart = new MimeMultipart();
					multipart.addBodyPart(body);
					message.setContent(multipart);
					//获取发送协议
					Transport transport = session.getTransport("smtp");
					//连接到邮件服务器
					//					transport.connect(PropertiesUtils.getHost(),
					//							PropertiesUtils.getUserName(), PropertiesUtils.getPassword());
					//					transport.connect("smtp.qq.com",
					//							"136876873", "tcq15108239710");
					transport.connect(mailHost, mailUserName,mailPassword);
					//发送邮件
					transport.sendMessage(message, message.getAllRecipients());
					//关闭发送端口
					transport.close();
					System.out.println("发送成功");
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		});
	}
	/**
	 * 生成html模板字符串
	 * @param root 存储动态数据的map
	 * @return
	 */
	private String getMailText(Map<String,Object> root, String templateName){
		String htmlText="";
		try {
			//通过指定模板名获取FreeMarker模板实例
			Template tpl=freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
			htmlText= FreeMarkerTemplateUtils.processTemplateIntoString(tpl,root);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return htmlText;
	}
}
